package teleradiologia.repository;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import teleradiologia.domain.Address;
import teleradiologia.domain.Doctor;
import teleradiologia.domain.Technician;
import teleradiologia.utils.HibernateUtil;

public class DoctorDaoImpl implements DoctorDao {
	private org.springframework.security.providers.encoding.BaseDigestPasswordEncoder encoder = new Md5PasswordEncoder();

	@Override
	public String create(Doctor doctor) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		doctor.getPerson().setPassword(
				encoder.encodePassword(doctor.getPerson().getPassword(),
						doctor.getPerson().getUsername()));

		String nip = (String) session.save(doctor);

		tx.commit();
		session.close();

		return nip;

	}

	@Override
	public void delete(Doctor doctor) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(doctor);

		tx.commit();
		session.close();

	}

	@Override
	public Doctor getByNip(String nip) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Doctor doctor = (Doctor) session.get(Doctor.class, nip);

		tx.commit();
		session.close();

		return doctor;

	}

	@Override
	public void update(Doctor doctor) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		if (doctor.getPerson().getPassword().length() < 20) {

			doctor.getPerson().setPassword(
					encoder.encodePassword(doctor.getPerson().getPassword(),
							doctor.getPerson().getUsername()));
		}

		session.update(doctor);

		tx.commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Doctor> getAll() {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Doctor> doctors = session.createQuery("from Doctor").list();

		tx.commit();
		session.close();

		return doctors;
	}

	@Override
	public Doctor getByIdPerson(long idPerson) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Doctor doctor = (Doctor) session.createQuery(
				"from Doctor doctor where doctor.person.id = :idPerson")
				.setLong("idPerson", idPerson).uniqueResult();

		tx.commit();
		session.close();

		return doctor;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Doctor> getAllByKrsnumber(String krsnumber) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Doctor> doctors = session
				.createQuery(
						"from Doctor doctor where doctor.diagnosticlab=:krsnumber order by doctor.person.surname asc")
				.setString("krsnumber", krsnumber).list();

		tx.commit();
		session.close();

		return doctors;
	}
}
